解析utf8字元(中文)
發生
Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
沒想到經過網址get傳參數
編碼後的"+",這個字元會因為searchParams.get(),而導致被替換成" "(空白字元)
// 使用utf-8字符集进行base64编码
function utoa(Object) {
btoa(unescape(encodeURIComponent(JSON.stringify(Object))))
}
// https://example.com?data=eyJ0aXRsZSI6IuaWsOWMl+eSsOeLgOe3muW9ouixoeW7o+WRiiJ9
// 使用utf-8字符集解析base64字符串
function atou() {
githubURL = new URL(window.location);
JSON.parse(decodeURIComponent(escape(atob(
githubURL
.searchParams
.get('data')
.replace(/-| /g, "+")
.replace(/_/g, "/")
.replace(/%/g, '%25')
))));
}
參考來源: